﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Boomer.Source.FrameWork.State_and_Screen_Manager.Screen_Manager;
using FrameWork.Source.Texture;
using FrameWork.Source.Resource_Manager;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using FrameWork.Source.CMenu_class;
using FrameWork.Source.Global_variables;
using FrameWork.Source.FrameWork;
using Microsoft.Xna.Framework.Media;

namespace Boomer.Source.FrameWork.State_and_Screen_Manager.State.MenuState.MenuScreen
{
    class OptionScreen : IScreen
    {
        private CTexture m_pOptionBG;

        private CTexture checkedImage;

        private CMenu menu;

        public OptionScreen(int index, string screenName, bool isActive, ScreenManager manager)
            : base(index, screenName, isActive, manager)
        {
            m_pOptionBG = null;
            menu = null;
            checkedImage = null;
        }

        public override void init()
        {
            m_pOptionBG = CResourceManager.getInstance().getImage((int)CResourceManager.IMG_MENU.OPTION);

            CMenuItem pIsMusic = new CMenuItem();
            pIsMusic.createItem("is music", null, 300, 60, new Vector2(190, 164), true);

            CMenuItem pIsSound = new CMenuItem();
            pIsSound.createItem("is sound", null, 300, 60, new Vector2(190, 256), true);

            CMenuItem pIsFullScreen = new CMenuItem();
            pIsFullScreen.createItem("is fullscreen", null, 400, 60, new Vector2(183, 338), true);

            CMenuItem pBack = new CMenuItem();
            pBack.createItem("back", null, 300, 65, new Vector2(236 , 489), true);

            menu = new CMenu();
            menu.addItem(pIsMusic);
            menu.addItem(pIsSound);
            menu.addItem(pIsFullScreen);
            menu.addItem(pBack);


            checkedImage = CResourceManager.getInstance().getImage((int)CResourceManager.IMG_MENU.CHECKED);

            base.init();
        }

        public override void update(float deltaTime)
        {
            base.update(deltaTime);
        }

        public override void render()
        {
            m_pOptionBG.drawImage(new Microsoft.Xna.Framework.Vector2(0, 0), Color.White);


            if (CGLOBAL.g_isPlayingMusic)
            {
                checkedImage.drawImage(new Vector2(402, 163), Color.White);
            }
            if (CGLOBAL.g_isPlayingSound)
            {
                checkedImage.drawImage(new Vector2(402, 260), Color.White);
            }
            if (CGLOBAL.g_isFullScreen)
            {
                checkedImage.drawImage(new Vector2(511, 340), Color.White);
            }

            base.render();
        }

        public override void processInput(float deltaTime)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                this.m_pScreenManager.setScreen("mainmenu screen", false, true);
            }


            MouseState mouse = Mouse.GetState();

            if (mouse.LeftButton == ButtonState.Pressed && CGLOBAL.g_isReleaseKey)
            {
                CGLOBAL.g_isReleaseKey = false;
                CMenuItem pItem = null;
                menu.picking(mouse.X, mouse.Y, ref pItem);
                if (pItem != null)
                {
                    if (pItem.m_strName == "is music")
                    {
                        CGLOBAL.g_isPlayingMusic = !CGLOBAL.g_isPlayingMusic;
                        if (!CGLOBAL.g_isPlayingMusic)
                        {
                            MediaPlayer.Pause();
                        }
                        else
                        {
                            MediaPlayer.Resume();
                        }
                    }
                    if (pItem.m_strName == "is sound")
                    {
                        CGLOBAL.g_isPlayingSound = !CGLOBAL.g_isPlayingSound;
                    }
                    if (pItem.m_strName == "is fullscreen")
                    {
                        CGLOBAL.g_isFullScreen = !CGLOBAL.g_isFullScreen;
                        StateManager.initCustomScreen(null, CGLOBAL.g_isFullScreen, CGLOBAL.g_iScreenWidth, CGLOBAL.g_iScreenHeight);
                    }
                    if (pItem.m_strName == "back")
                    {
                        this.m_pScreenManager.setScreen("mainmenu screen", false, true);
                    }
                }
            }


            if (mouse.LeftButton == ButtonState.Released)
            {
                CGLOBAL.g_isReleaseKey = true;
            }


            base.processInput(deltaTime);
        }
    }
}
